﻿using System;
using System.Linq;
using System.Linq.Expressions;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using System.Text.RegularExpressions;

namespace SBACN.WebSite.Education
{
	public partial class PersonalExamSearch : MemberBasePage
	{
		public string Name { get; set; }
		public string Member { get; set; }
		public string CID { get; set; }
		public int PageFlag { get; set; }
		public string SystemType
		{
			get
			{
				return this.SystemCode.ToString();
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{

			if (!Page.IsPostBack && !string.IsNullOrWhiteSpace(Request.QueryString["id"]))
			{
				string CardID = Request.QueryString["id"].Trim();
				Regex rgStr = new Regex(@"^(\w)*(-)*(\w)*$");
				if (rgStr.IsMatch(CardID))
				{
					this.CID = CardID;
					CheckCID();
					TitShow();
					ShowList(1);
					
				}
				else
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrMsg", "<script>alert('请输入正确的身份证号码！');window.location='/Education/PersonalExamSearch.aspx?st="+this.SystemType+"';</script>");
					//this.Msg.Visible = true;
					//this.CID = CardID;
					return;
				}
			}
		}

		protected void CheckCID()
		{
			Expression<Func<TB_Score, bool>> f = (p => p.IDCardNO == this.CID);
			Expression<Func<TB_Score, int>> order = (p => p.ID);
			var resultQ = DAL.DALHelper.Instance.GetList<TB_Score, int>(f, order, false);
			if (resultQ.Count <= 0)
			{
				this.Msg.Visible = true;
				return;
			}
		}

		protected void TitShow()
		{
			Expression<Func<TB_Score, bool>> f = (p => p.IDCardNO == this.CID);
			Expression<Func<TB_Score, int>> order = (p => p.ID);
			var resultQ = DAL.DALHelper.Instance.GetList<TB_Score, int>(f, order, false).FirstOrDefault();
			if (resultQ != null)
			{
				this.Name = resultQ.Name;
				Expression<Func<TB_Member, bool>> f1 = (p => p.ID == resultQ.MemberID);
				var resultM = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_Member>(f1);
				if (resultM != null)
					this.Member = resultM.Name;
			}
		}

		protected void ShowList(int pageIndex)
		{
			this.PageFlag = (pageIndex - 1) * this.Pager1.PageSize;
			int records = 0;
			Expression<Func<View_ExamScore, bool>> f = (p => p.IDCardNO == this.CID && p.Status == 0);
			Expression<Func<View_ExamScore, DateTime>> order = (c => c.Deadline);
			var resultQ = DAL.DALHelper.Instance.GetList<View_ExamScore, DateTime>(f, order, false, this.Pager1.PageSize, pageIndex, out records).Select(p => new { Deadline = p.Deadline, Subject = p.Subject, Score = string.IsNullOrEmpty(p.Score) ? "政策:" + p.ScorePolicy + "<br /> 业务:" + p.ScoreProfession : p.Score, Pass = (string.IsNullOrEmpty(p.Score) ? IsPass(p.ScorePolicy) && IsPass(p.ScoreProfession) : IsPass(p.Score)) });

			this.List.DataSource = resultQ;
			this.List.DataBind();
			this.Pager1.RecordCount = records;
			this.Pager1.InitControl();
		}

		private bool IsPass(string score)
		{
			int s = 0;
			if (int.TryParse(score, out s))
			{
				return s >= 60;
			}
			return false;
		}

		protected void BindSelectedPageIndexChanged(int pageIndex, int nextPage)
		{
			ShowList(nextPage);
		}

		//public string ExamName(string id)
		//{
		//    int ID = Convert.ToInt32(id);
		//    var result = DAL.DALHelper.Instance.GetObject<TB_Examination>("ID", ID);

		//    return result.Subject;
		//}

		//public string ExamDate(string id)
		//{
		//    int ID = Convert.ToInt32(id);
		//    var result = DAL.DALHelper.Instance.GetObject<TB_Examination>("ID", ID);

		//    return result.Deadline.ToString("yyyy-MM-dd");
		//}

		public string Color(string pass)
		{
			bool p;
			if (bool.TryParse(pass, out p) && p)
				return "color='#009933'";
			else
				return "color='#FF0000'";
		}
	}
}